/*
 * Copyright (C) 2012 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.gui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import ch.bfh.aeneas.R;

/**
 * A specific activity to get an input from the user. As soon as the user hits
 * the "ok" button, setResult gets called with an intent as parameter that 
 * contains the entered as an extra with name 'input'.
 * 
 * If the user hits the "cancel" button, no data will be passed before calling
 * finish().
 * 
 * You may let the activity display a numeric keyboard instead of a alphabetic 
 * one by passing a boolean extra with name 'numeric' set to true.
 * 
 * If you want the activity to display a title, put the desired title as an
 * extra with name 'title' into the starting intent. Be aware, that the title
 * of the window gets read by TTS!
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class StringInput extends Activity implements OnClickListener {

    private static final String LOG_TAG = "StringInput";
    private Button mOkBtn;
    private Button mCancelBtn;
    private EditText mInputField;
    private boolean mIsNumeric;
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        setContentView(R.layout.string_input);
        
        Intent i = getIntent();
        String title = i.getStringExtra("title");
        Log.i(LOG_TAG, "Displaying list of elements ("+title+") to pick from");
        
        String defaultValue = i.getStringExtra("default");
        if(defaultValue == null) {
            defaultValue = "";
        }
        
        mIsNumeric = i.getBooleanExtra("numeric", false);
        
        super.setTitle(title);
        
        mOkBtn = (Button) findViewById(R.id.string_input_finish);
        mOkBtn.setOnClickListener(this);
        mCancelBtn = (Button) findViewById(R.id.string_input_cancel);
        mCancelBtn.setOnClickListener(this);
        mInputField = (EditText) findViewById(R.id.string_input_field);
        mInputField.setText(defaultValue);
        
        if(mIsNumeric) {
            mInputField.setKeyListener(new DigitsKeyListener());
        }
    }
    
    private void postAnswer() {
        Intent i = new Intent();
        String input = mInputField.getText().toString();
        if(!mIsNumeric) {
            i.putExtra("input", input);
        } else {
            int conv = Integer.decode(input);
            i.putExtra("input", conv);
        }
        setResult(Activity.RESULT_OK, i);
        finish();
    }
    
    private void postCancel() {
        setResult(Activity.RESULT_CANCELED);
        finish();
    }

    /**
     * {@inheritDoc}
     */
    public void onClick(View arg0) {
        if(arg0 == mOkBtn) {
            postAnswer();
        } else if(arg0 == mCancelBtn) {
            postCancel();
        }
    }
}
